using Joy.Erp.BasicArchives.Core.Filters;
using Joy.Erp.BasicArchives.HttpApi.SimplyClient.Core;
using Joy.Erp.BasicArchives.ItemMasters;
using System;
using System.Threading.Tasks;

namespace Joy.Erp.BasicArchives.HttpApi.SimplyClient.ItemMasters
{
    public class BomMasterClientProxy
    {
        private readonly TokenClientProxy _tokenClientProxy;

        public BomMasterClientProxy(TokenClientProxy tokenClientProxy)
        {
            _tokenClientProxy = tokenClientProxy;
        }

        public async Task<BomMasterDto> GetAsync(Guid id, Action<string> logfunc = null)
        {
            var token = await _tokenClientProxy.GetToken(logfunc);
            var url = _tokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/bom-master/{id}");

            return await ApiHandler.HttpGetAsync<BomMasterDto>(token, url, logfunc);
        }

        public BomMasterDto Get(Guid id, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await GetAsync(id, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<BomComponentDto> GetChildAsync(Guid id, Action<string> logfunc = null)
        {
            var token = await _tokenClientProxy.GetToken(logfunc);
            var url = _tokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/bom-master/child/{id}");

            return await ApiHandler.HttpGetAsync<BomComponentDto>(token, url, logfunc);
        }

        public BomComponentDto GetChild(Guid id, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await GetChildAsync(id, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<BomMasterDto> FindAsync(string filter, Action<string> logfunc = null)
        {
            var token = await _tokenClientProxy.GetToken(logfunc);
            var url = _tokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/bom-master/find?filter={filter}");

            return await ApiHandler.HttpGetAsync<BomMasterDto>(token, url, logfunc);
        }

        public BomMasterDto Find(string filter, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await FindAsync(filter, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<BomMasterDto> FindBySourceKeyAsync(string sourceKey, string sourceType, Action<string> logfunc = null)
        {
            var token = await _tokenClientProxy.GetToken(logfunc);
            var url = _tokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/bom-master/find-by-source-key?sourceKey={sourceKey}&sourceType={sourceType}");

            return await ApiHandler.HttpGetAsync<BomMasterDto>(token, url, logfunc);
        }

        public BomMasterDto FindBySourceKey(string sourceKey, string sourceType, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await FindBySourceKeyAsync(sourceKey, sourceType, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<PagedResultDto<BomMasterDto>> GetListAsync(FilterRequestDto input, Action<string> logfunc = null)
        {
            var token = await _tokenClientProxy.GetToken(logfunc);
            var url = _tokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/bom-master?filter={input.Filter}&sorting={input.Sorting}&skipCount={input.SkipCount}&maxResultCount={input.MaxResultCount}");

            return await ApiHandler.HttpGetAsync<PagedResultDto<BomMasterDto>>(token, url, logfunc);
        }

        public PagedResultDto<BomMasterDto> GetList(FilterRequestDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await GetListAsync(input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<PagedResultDto<BomMasterDto>> QueryAsync(FilterGroupRequestDto input, Action<string> logfunc = null)
        {
            var token = await _tokenClientProxy.GetToken(logfunc);
            var url = _tokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/bom-master/query");

            return await ApiHandler.HttpPostAsync<PagedResultDto<BomMasterDto>>(token, url, input, logfunc);
        }

        public PagedResultDto<BomMasterDto> Query(FilterGroupRequestDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await QueryAsync(input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<PagedResultDto<BomComponentDto>> QueryChildrenAsync(FilterGroupRequestDto input, Action<string> logfunc = null)
        {
            var token = await _tokenClientProxy.GetToken(logfunc);
            var url = _tokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/bom-master/query-children");

            return await ApiHandler.HttpPostAsync<PagedResultDto<BomComponentDto>>(token, url, input, logfunc);
        }

        public PagedResultDto<BomComponentDto> QueryChildren(FilterGroupRequestDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await QueryChildrenAsync(input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<BomMasterDto> CreateAsync(BomMasterCreateDto input, Action<string> logfunc = null)
        {
            var token = await _tokenClientProxy.GetToken(logfunc);
            var url = _tokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/bom-master");

            return await ApiHandler.HttpPostAsync<BomMasterDto>(token, url, input, logfunc);
        }

        public BomMasterDto Create(BomMasterCreateDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await CreateAsync(input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<BomMasterDto> UpdateAsync(Guid id, BomMasterUpdateDto input, Action<string> logfunc = null)
        {
            var token = await _tokenClientProxy.GetToken(logfunc);
            var url = _tokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/bom-master/{id}");

            return await ApiHandler.HttpPutAsync<BomMasterDto>(token, url, input, logfunc);
        }

        public BomMasterDto Update(Guid id, BomMasterUpdateDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await UpdateAsync(id, input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<BomMasterDto> SubmitAsync(Guid id, Action<string> logfunc = null)
        {
            var token = await _tokenClientProxy.GetToken(logfunc);
            var url = _tokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/bom-master/submit?id={id}");

            return await ApiHandler.HttpPostAsync<BomMasterDto>(token, url, logfunc);
        }

        public BomMasterDto Submit(Guid id, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await SubmitAsync(id, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<BomMasterDto> ApproveAsync(Guid id, Action<string> logfunc = null)
        {
            var token = await _tokenClientProxy.GetToken(logfunc);
            var url = _tokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/bom-master/approve?id={id}");

            return await ApiHandler.HttpPostAsync<BomMasterDto>(token, url, logfunc);
        }

        public BomMasterDto Approve(Guid id, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await ApproveAsync(id, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<BomMasterDto> UnApproveAsync(Guid id, Action<string> logfunc = null)
        {
            var token = await _tokenClientProxy.GetToken(logfunc);
            var url = _tokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/bom-master/unapprove?id={id}");

            return await ApiHandler.HttpPostAsync<BomMasterDto>(token, url, logfunc);
        }

        public BomMasterDto UnApprove(Guid id, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await UnApproveAsync(id, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task DeleteAsync(Guid id, Action<string> logfunc = null)
        {
            var token = await _tokenClientProxy.GetToken(logfunc);
            var url = _tokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/bom-master/{id}");

            await ApiHandler.HttpDeleteAsync(token, url, logfunc);
        }

        public void Delete(Guid id, Action<string> logfunc = null)
        {
            try
            {
                Task.Run(async () => await DeleteAsync(id, logfunc));
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }
    }
}